package com.piwik.android;

import java.util.logging.ConsoleHandler;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

import org.achartengine.ChartFactory;
import org.achartengine.charts.BarChart;
import org.achartengine.charts.IChart;

import com.piwik.android.R;

public class AndroidMain extends Activity {
    /** Called when the activity is first created. */
	private HttpHelper piwikConnector = new HttpHelper();
	private XmlHelper xmlHelper = new XmlHelper();
	private IChart iChart = new BarChart();
	private PiwikDbAdapter mDbHelper;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
          
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.confirm).setTitle(R.string.confirm).setPositiveButton("Done", 
        		new DialogInterface.OnClickListener(){
        		public void onClick(DialogInterface dialog, int id){
        			dialog.dismiss();
        		}
        	
        });
        
        final AlertDialog alert = builder.create();
        
        Button b = (Button) findViewById(R.id.confirm);
        b.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v)
    		{
    			buttonOnClickEventHandler(v);
    		}
        });
        
        //TODO: fetch the stored userName and password if its exists
        /*mDbHelper = new PiwikDbAdapter(this);
    	mDbHelper.open();
    	Bundle bundle = new Bundle();
        Cursor mCursor = mDbHelper.fetchData(0);
        EditText url = (EditText)findViewById(R.id.url);
		EditText uName = (EditText)findViewById(R.id.username);
		EditText pwd = (EditText)findViewById(R.id.password);
		*/
		
    }
    
    public void buttonOnClickEventHandler(View v){
    	//TODO: Chnage this
		EditText url = (EditText)findViewById(R.id.url);
		EditText uName = (EditText)findViewById(R.id.username);
		EditText pwd = (EditText)findViewById(R.id.password);
		CheckBox rememberMe = (CheckBox)findViewById(R.id.rememberMe);
		    			  			
		String userName = uName.getText().toString();
		String passwd = pwd.getText().toString();
		String urlName = url.getText().toString();
		//get token auth over http
		String tokenAuth = piwikConnector.getTokenAuth(userName, passwd);
		/*TODO: Make this work later
		 * if(rememberMe.isChecked()){
			//Store the username, passwd, tokenAuth in the DB, this will be used for further 
			//communications with the server
			createDBConnection(userName, passwd,urlName, tokenAuth );
		}*/
		
		//create the http connection over the internet
		String response = piwikConnector.getListOfSites(tokenAuth);
		//TODO: use xml parser instead
		//setContentView(R.layout.sites);
		//TableLayout tl = (TableLayout)findViewById(R.id.siteListTableLayout);
		/*TableLayout tl = new TableLayout(this);
		displaySiteInformation(response, tl);
		setContentView(tl);
		*/
		drawChart();
		
		/*setContentView(R.layout.welcome);
		TextView t = (TextView)findViewById(R.id.TextView01);
		t.setText(response);
		*/
    }
    public void displaySiteInformation(String response, TableLayout tl){
    	String[] responseStrings = response.split("\n");
    	String[] headerString = responseStrings[0].split(",");
    	String[] bodyString = responseStrings[1].split(",");
    	
    	
    	for(int i=0; i<headerString.length -1; i++){
    		TableRow tr = new TableRow(this);
    		tr.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT)); 
    		
    		TextView header = new TextView(this);
    		header.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));
    		header.setText(headerString[i].toString());
    		
    		TextView body = new TextView(this);
    		body.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));
    		if(bodyString[i]!= null)
    			body.setText(bodyString[i].toString()); 
    		else
    			body.setText("N/A");
    		
    		tr.addView(header);
    		tr.addView(body);
    	tl.addView(tr, new TableLayout.LayoutParams(
    			 LayoutParams.FILL_PARENT, 
                 LayoutParams.WRAP_CONTENT
    				));	
    	
    	}
    	
    	
    	//setContentView(R.layout.sites); 
    }
    
    
    public void createDBConnection(String userName, String passwd, String urlName, String tokenAuth){
    	mDbHelper = new PiwikDbAdapter(this);
    	mDbHelper.open();
    	mDbHelper.storeData(urlName, userName, passwd, tokenAuth);
    }
    
    public void drawChart(){
    	Intent intent = iChart.execute(this);
    	startActivity(intent);
    }
    
}